import 'dart:core';

import 'package:amway_superapp_creator_plugin/models/create_model_config_model.dart';
import 'package:amway_superapp_creator_plugin/models/create_user_info_model.dart';
import 'package:amway_superapp_creator_plugin/net/http_manager.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/base_model.dart';
import 'package:flutter/cupertino.dart';

import '../../api.dart';
import '../../view_state.dart';

class CreateHomeViewModel extends BaseModel {
  CreateUserInfoModel _createUserInfoModel;
  String _shareLink;


  String get shareLink => _shareLink;

  set shareLink(String value) {
    _shareLink = value;
    notifyListeners();
  }

  CreateHomeViewModel({@required Api api}) : super(api: api);

  Future<void> getUserInfo(String amwayAda, {bool isVisible = true}) {
    isVisible == true ? setState(ViewState.Busy) : setState(ViewState.Idle);
    HttpManager().get(
        url: "/apis/v1/creator/personal-configs/$amwayAda",
        tag: null,
        successCallback: (data) {
          setState(ViewState.Idle);
          if (data != null) {
            createUserInfoModel = CreateUserInfoModel.fromJson(data);
          }
        },
        errorCallback: (data) {
          setState(ViewState.Idle);
          createUserInfoModel = null;
        });
  }

  bool isOpenCreate = false;
  bool isUserInfoReviewState = false;
  bool isAutoShowShare = false;

  void showCreateBootSheet(HttpSuccessCallback callback) async {
    await Future.delayed(Duration(seconds: 2))
        .then((value) => callback.call(null));
  }

  String getUserHeadUrl() {
    if (createUserInfoModel != null &&
        createUserInfoModel.personalSign != null &&
        createUserInfoModel.personalSign.data != null) {
      return createUserInfoModel.personalSign.data;
    }

    return "https://ft2-creatorconf-oss.oss-cn-shenzhen.aliyuncs.com/Creators_As_20201023/PNG/A__Art- &-Crafts.png";
  }

  CreateUserInfoModel get createUserInfoModel => _createUserInfoModel;

  set createUserInfoModel(CreateUserInfoModel value) {
    _createUserInfoModel = value;
    if (_createUserInfoModel != null &&
        _createUserInfoModel.nick != null &&
        _createUserInfoModel.nick.length > 0) {
      List<DisplayAuditsBean> displayAudits = createUserInfoModel.displayAudits;
      if (displayAudits != null && displayAudits.length > 0) {
        displayAudits.forEach((element) {
          if (element.data.isNotEmpty &&
              element.data == createUserInfoModel.nick) {
            if (element.status == "UN_PASS") {
              isOpenCreate = false;
            } else {
              isOpenCreate = true;
            }
          }
        });
      }
    }
    if (isOpenCreate) {
      if (_createUserInfoModel != null &&
          _createUserInfoModel.auditStatus != null) {
        if (_createUserInfoModel.auditStatus == "UN_PASS") {
          isUserInfoReviewState = true;
        } else {
          isUserInfoReviewState = false;
        }
      }
    }

    notifyListeners();
  }


  void queryShareLink({String amwayAda, String type = 'h5'}) {
    HttpManager().get(
        url: "/apis/v1/creator/index/getStationUrl/${amwayAda}/${type}",
        tag: null,
        successCallback: (data) {
          shareLink = data;
        },
        errorCallback: (data) {
          print(data);
        });
  }


  bool _isUpassModel = false;

  bool get isUpassModel => _isUpassModel;

  set isUpassModel(bool value) {
    _isUpassModel = value;
    notifyListeners();
  }

  CreateModelConfigModel _createModelConfigModel;

  CreateModelConfigModel get createModelConfigModel => _createModelConfigModel;

  set createModelConfigModel(CreateModelConfigModel value) {
    _createModelConfigModel = value;
    if (isOpenCreate) {
      if (null != createModelConfigModel &&
          null != createModelConfigModel.moduleItemList &&
          createModelConfigModel.moduleItemList.length > 0) {
        createModelConfigModel.moduleItemList.forEach((modelItem) {
          if (modelItem.auditStatusEnum == "UN_PASS") {
            isUpassModel = true;
            return;
          }
        });
      }
    }
  }

  void queryModelConfiginfo(String amwayAda) {
    HttpManager().get(
        url: "/apis/v1/creator/module-configs/${amwayAda}",
        tag: null,
        successCallback: (data) {
          createModelConfigModel = CreateModelConfigModel
              .fromJson(data);
        },
        errorCallback: (data) {
          print(data);
        });
  }


}
